package org.rcgwt.sample.client.richcalendar;

import java.util.Date;

import org.rcgwt.client.ui.calendar.RichCalendar;
import org.rcgwt.client.ui.calendar.event.SelectionChangedEvent;
import org.rcgwt.client.ui.calendar.event.SelectionChangedHandler;
import org.rcgwt.client.ui.calendar.model.impl.Event;
import org.rcgwt.client.ui.calendar.model.impl.EventColor;
import org.rcgwt.client.ui.calendar.model.impl.EventFrequency;
import org.rcgwt.client.ui.calendar.model.impl.EventType;
import org.rcgwt.client.ui.calendar.model.impl.SelectionCardinality;
import org.rcgwt.client.ui.calendar.synchronization.GoogleAgendaSynchronizer;
import org.rcgwt.client.ui.themes.StandardTheme.RendererLevel;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class RichCalendarExample implements EntryPoint {

	@Override
	public void onModuleLoad() {

		final DateTimeFormat format = DateTimeFormat.getShortDateFormat();
		final RootPanel rp = RootPanel.get();
		rp.addStyleName("richCalendar-rootPanel");

		final FlexTable calendarGrid = new FlexTable();

		final Label title = new Label("RichCalendar Component ShowCase");
		title.addStyleName("richCalendar-test-title");

		/* Samples about render level */

		final Label title_sample1 = new Label("1. RENDERER TYPES");

		final Label title_rc0 = new Label("RENDERER : SIMPLE");
		final Label title_rc1 = new Label("RENDERER : SHORT");
		final Label title_rc2 = new Label("RENDERER : MEDIUM");
		final Label title_rc3 = new Label(
				"RENDERER : FULL (Synchronized with Google Calendar)");
		final Label title_rc4 = new Label(
				"RENDERER : FULL (Without week numbers bar)");

		final RichCalendar rc0 = new RichCalendar(RendererLevel.SIMPLE,
				SelectionCardinality.MULTIPLE, true, true);
		final RichCalendar rc1 = new RichCalendar(RendererLevel.SHORT,
				SelectionCardinality.MULTIPLE, true, true);
		final RichCalendar rc2 = new RichCalendar(RendererLevel.MEDIUM,
				SelectionCardinality.MULTIPLE, true, true);
		final RichCalendar rc3 = new RichCalendar(RendererLevel.FULL,
				SelectionCardinality.MULTIPLE, true, true);
		final RichCalendar rc4 = new RichCalendar(RendererLevel.FULL,
				SelectionCardinality.MULTIPLE, false, true);
		final RichCalendar rc5 = new RichCalendar(RendererLevel.POPUP,
				SelectionCardinality.MULTIPLE, true, true);

		final Label footer = new Label(
				"2010 - RCGWT - VEDANA - http://www.vedana.com");

		rc3.createEvent(new Event("Sortie en mer",
				"Sortie en mer avec Gilbert", "06/8/2010 13:00",
				"06/9/2010 13:00", "En mer", EventType.HOLIDAYS,
				EventFrequency.NONE, EventColor.NONE));

		/* Min & Max dates */

		rc0.setMinDate(new Date(110, 4, 15));
		rc0.setMaxDate(new Date(110, 7, 15));

		rc1.setMinDate(new Date(110, 4, 1));
		rc1.setMaxDate(new Date(110, 7, 1));

		rc2.setMinDate(new Date(110, 4, 1));
		rc2.setMaxDate(new Date(112, 7, 1));

		rc3.setMinDate(new Date(110, 4, 15));
		rc3.setMaxDate(new Date(111, 7, 15));

		rc5.setMinDate(new Date(110, 4, 1));
		rc5.setMaxDate(new Date(113, 7, 1));

		final GoogleAgendaSynchronizer synchronizer = GWT
				.create(GoogleAgendaSynchronizer.class);

		synchronizer
				.synchronize(
						"http://www.google.com/calendar/feeds/elian.oriou%40gmail.com/private-0f9c31f124fa919c9e4ad26d5060ba49/basic",
						rc3);

		// synchronizer.synchronizeWithAuthentication("Elian ORIOU", rc4);

		/* Samples about selection type */

		final Label title_sample2 = new Label("2. SELECTION TYPES");

		final Label title_rc1bis = new Label("SELECTION : NONE");
		final Label title_rc2bis = new Label("SELECTION : SINGLE");
		final Label title_rc3bis = new Label("SELECTION : MULTIPLE");

		final RichCalendar rcbis = new RichCalendar(RendererLevel.FULL,
				SelectionCardinality.NONE, true, false);
		final RichCalendar rc2bis = new RichCalendar(RendererLevel.FULL,
				SelectionCardinality.SINGLE, true, true);
		final RichCalendar rc3bis = new RichCalendar(RendererLevel.FULL,
				SelectionCardinality.MULTIPLE, true, true);

		Label l_single = new Label("Selected Value : ");
		final TextBox tb_single = new TextBox();
		rc2bis.addValueChangeHandler(new ValueChangeHandler<Date>() {
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				Date d = event.getValue();
				tb_single.setValue(format.format(d));
			}
		});

		Label l_multiple = new Label("Selected Dates : ");
		final VerticalPanel vp = new VerticalPanel();
		rc3bis.addSelectionChangedHandler(new SelectionChangedHandler() {
			@Override
			public void onSelectionChanged(SelectionChangedEvent event) {
				vp.clear();
				for (Date d : event.getDates()) {
					Label l = new Label(format.format(d));
					vp.add(l);
				}
			}
		});

		/* Positioning */

		calendarGrid.setWidget(0, 0, title_sample1);

		calendarGrid.setWidget(1, 0, title_rc0);
		calendarGrid.setWidget(1, 1, title_rc1);
		calendarGrid.setWidget(1, 2, title_rc2);
		calendarGrid.setWidget(1, 3, title_rc3);
		calendarGrid.setWidget(1, 4, title_rc4);

		calendarGrid.setWidget(2, 0, rc0);
		calendarGrid.setWidget(2, 1, rc1);
		calendarGrid.setWidget(2, 2, rc2);
		calendarGrid.setWidget(2, 3, rc3);
		calendarGrid.setWidget(2, 4, rc4);
		calendarGrid.setWidget(2, 5, rc5);

		calendarGrid.setWidget(3, 0, title_sample2);

		calendarGrid.setWidget(4, 0, title_rc1bis);
		calendarGrid.setWidget(4, 1, title_rc2bis);
		calendarGrid.setWidget(4, 2, title_rc3bis);

		calendarGrid.setWidget(5, 0, rcbis);
		calendarGrid.setWidget(5, 1, rc2bis);
		calendarGrid.setWidget(5, 2, rc3bis);

		calendarGrid.setWidget(6, 1, l_single);
		calendarGrid.setWidget(7, 1, tb_single);

		calendarGrid.setWidget(6, 2, l_multiple);
		calendarGrid.setWidget(7, 2, vp);

		rp.add(title);
		rp.add(calendarGrid);
		rp.add(footer);
	}
}